package com.jd.header;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import java.io.IOException;

public class MediaPlayerManager {

    private MediaPlayer mediaPlayer;
    public static MediaPlayerManager instance = null;
    private Handler handler;
    private String musicPath = "";

    public static MediaPlayerManager getInstance() {
        if (instance == null) {
            synchronized (MediaPlayerManager.class) {
                if (instance == null) {
                    instance = new MediaPlayerManager();
                }
            }
        }
        return instance;
    }

    public MediaPlayer getMediaPlayer() {
        return mediaPlayer;
    }

    public void start(Context context, String filePath, OnPlayLister playLister) {
        try {
            if (TextUtils.isEmpty(filePath)) {
                return;
            }
            if (mediaPlayer == null) {
                mediaPlayer = new MediaPlayer();
            }

            if (!musicPath.equals(filePath)) {
                Uri myUri = Uri.parse(filePath);
                musicPath = filePath;
                mediaPlayer.setDataSource(context, myUri);
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mediaPlayer.prepare();
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        if (playLister != null) {
                            playLister.onCompletion(mp);
                        }
                    }
                });
                if (playLister != null) {
                    playLister.onPrepared(mediaPlayer);
                }
            }

            mediaPlayer.start();

            if (handler != null) {
                handler.removeCallbacksAndMessages(null);
                handler = null;
            }
            handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(mediaPlayer != null){
                        if (mediaPlayer.isPlaying()) {
                            if (playLister != null) {
                                playLister.onPlaying(mediaPlayer.getCurrentPosition(), mediaPlayer.getDuration());
                            }
                        }
                        handler.postDelayed(this, 1000);
                    }
                }
            }, 1000);
            Log.e("TAG", "开始播放");
        } catch (IOException e) {
            Log.e("TAG", "播放异常 " + e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * 播放指定位置
     *
     * @param duration
     */
    public void seekTo(int duration) {
        if (mediaPlayer == null) {
            return;
        }
        mediaPlayer.seekTo(duration);
        mediaPlayer.start();
    }

    /**
     * 暂停播放
     */
    public void pause() {
        if (mediaPlayer == null) {
            return;
        }
        if (mediaPlayer.isPlaying()) {
            Log.e("TAG", "停止播放");
            mediaPlayer.pause();
        }
    }

    /**
     * 停止播放
     */
    public void stop() {
        if (mediaPlayer == null) {
            return;
        }
        if (mediaPlayer.isPlaying()) {
            Log.e("TAG", "停止播放");
            mediaPlayer.stop();
        }
    }

    /**
     * 释放控制器
     */
    public void release() {
        musicPath = "";
        if (mediaPlayer == null) {
            return;
        }
        if(handler != null){
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
        mediaPlayer.release();
        mediaPlayer = null;
    }

    public String postions(int postion) {
        int musicTime = postion / 1000;
        String s = String.valueOf(musicTime / 60);
        String m = String.valueOf(musicTime % 60);

        if (s.length() < 2) {
            s = "0" + s;
        }
        if (m.length() < 2) {
            m = "0" + m;
        }
        return s + ":" + m;
    }

    public interface OnPlayLister {
        void onPlaying(int progress, int duration);

        void onPrepared(MediaPlayer mediaPlayer);

        void onCompletion(MediaPlayer mediaPlayer);
    }
}
